//
//  STTitleView.m
//  赛拓
//
//  Created by 王磊 on 8/7/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STTitleView.h"

@implementation STTitleView
- (void)commitInit {
    [super commitInit];
    
    self.font = ST_20_FONT;
    
}
- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [obj removeFromSuperview];
        
        obj = nil;
    }];
    for (int i = 0; i < titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.tag = i + 10;
        
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:ST_BLACK_COLOR forState:UIControlStateNormal];
        
        [btn addTarget: self action:@selector(btnTriggered:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btn];
    }
}
- (void)setFont:(UIFont *)font {
    _font = font;
    
    //    @WeakObj(self)
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.titleLabel.font = font;
    }];
}
- (void)btnTriggered:(UIButton *)sender {
    
    [self.mDelegate onItemClick:STItemClickTypeTitle withData:nil forIndexPath:[NSIndexPath indexPathForRow:sender.tag - 10 inSection:0]];
    
}

- (void)updateData:(id)data forIndex:(NSInteger)index {

    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (index == idx) {
            [obj setTitle:data forState:UIControlStateNormal];
        }
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    @WeakObj(self)
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.frame = CGRectMake(idx * selfWeak.k_width / selfWeak.titles.count, 0, selfWeak.k_width / selfWeak.titles.count, selfWeak.k_height);
        
    }];
}
@end
